/**
 * @(#) NoticeProxy.as
 *
 * @date		2009. 03. 11.
 * @version		1.0
 * @author		Park, JuneHo
 * @description	 공지사항 쓰기 - Proxy
 * 				공지사항 조회하는 Model Logic 
 */
package com.skt.common_hr.mintview.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.mintview.MintViewFacade;
	import com.skt.common_hr.mintview.model.vo.NoticeDetailVO;
	import com.skt.common_hr.mintview.model.vo.NoticeVO;
	import com.skt.common_hr.mintview.model.vo.mintListVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.AlertHelper;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class NoticeWriteProxy extends CommonProxy
	{
		public static const NAME:String = "NoticeWriteProxy";
		
		public var noticeListArr:NoticeVO;
		public var noticeFileArr:ArrayCollection;
		public var writeKey:String;


		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function NoticeWriteProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		public function loadSearch(vo:mintListVO):void
		{
			var params:Object = new Object();
			var tables:Object = new Object();
			var table:Array = new Array();
			params["I_ACTIO"] = "DIS";
			
			table.push(toJsonType(vo));
			//table[0] = vo;
			tables["ITAB"] = table;
			saveData("Z_HR_IN07",params, tables, result, loadKeyFault);
		}	
		
		public function update(record:NoticeDetailVO, arr:ArrayCollection):void
		{
			var params:Object = new Object();
			var paramsI:Object = new Object();
			var tables:Object = new Object();
			var table:Array = new Array();
			var tableFile:Array = new Array();
			
			paramsI["I_ACTIO"] 			= "SAVE";
			paramsI["I_CONTX"]			= record.CONTX;			
			
			params["ORGTX"]				= record.ORGTX;
			params["BEGDA"]				= DateUtil.toBasicString(record.BEGDA);
			params["PERNR"]				= record.PERNR;
			params["CDATE"]				= DateUtil.toBasicString(record.CDATE);
			params["ENAME"]				= record.ENAME;
			params["VNAME"]				= record.VNAME;
			params["JINFO"]				= record.JINFO;
			params["STIME"]				= DateUtil.toBasicTimeString(record.STIME);
			params["ETIME"]				= DateUtil.toBasicTimeString(record.ETIME);
			params["TITLE"]				= record.TITLE;
			params["CONTENT"]			= record.CONTENT;
			params["SEQNR"]				= record.SEQNR;
			params["AEDTM"]				= DateUtil.toBasicString(record.AEDTM);
			params["CDATE"]				= DateUtil.toBasicString(record.CDATE);
			params["CTIME"]				= DateUtil.toBasicTimeString(record.CTIME);
			params["DATUM"]				= DateUtil.toBasicString(record.DATUM);
			params["MANDT"]				= record.MANDT;
			params["SRTFD"]				= record.SRTFD;
			params["UZEIT"]				= DateUtil.toBasicTimeString(record.UZEIT);
			params["VNAME"]				= record.VNAME;
			params["VIEWR"]				= record.VIEWR;
			
			table.push(params);
			
			var obj:Object;
			for (var i:Number = 0 ;i <arr.length;i++){
				obj = {};
				obj.FILE_NAME = arr[i].FILE_NAME;
				obj.FILE_EXT = arr[i].FILE_EXT;
				obj.FILE_SIZE = arr[i].FILE_SIZE;
				obj.FILE_URL = arr[i].FILE_URL;
				tableFile.push(obj);
			}
			tables["ITAB"] = table;
			tables["ITABF"] = tableFile;
			
			saveData("Z_HR_IN07",paramsI, tables, updateResult, updateFault);
		}
		

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		 
		public function result(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				noticeListArr = parseWriteList(event.result.ITAB);
				noticeFileArr = event.result.ITABF; 
				facade.sendNotification(MintViewFacade.WRITE_SEARCH_NOTICE_COMPLETE, this);
			}
			else {
				trace(event.result["RETCODE"]+" : "+event.result["RETTEXT"]);
			}
			CursorManager.removeBusyCursor();
		} 
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function loadKeyFault(event:FaultEvent):void
		{
			facade.sendNotification(MintViewFacade.LOAD_KEY_FAILED, this);
			CursorManager.removeBusyCursor();
		}
		
		

		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		 
		public function insertResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				facade.sendNotification(MintViewFacade.INSERT_NOTICE_COMPLETE, this);
			}
			else {
				trace( "failed to insert article: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				AlertHelper.showMessage(event.result["RETTEXT"], "오류");
				facade.sendNotification(MintViewFacade.INSERT_NOTICE_FAILED, this);
			}
			CursorManager.removeBusyCursor();
		}
		

		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function insertFault(event:FaultEvent):void
		{
			trace("Fault message in noticeWriteProxy: " + event.fault.faultString);
			facade.sendNotification(MintViewFacade.INSERT_NOTICE_FAILED, this);
			CursorManager.removeBusyCursor();
		}


		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function updateResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				facade.sendNotification(MintViewFacade.UPDATE_NOTICE_COMPLETE, this);
			}
			else {
				trace(event.result["RETCODE"]+" : "+event.result["RETTEXT"]);
				Alert.show(event.result["RETTEXT"]);
//				facade.sendNotification(MintViewFacade.INSERT_NOTICE_FAILED, this);
			}
			CursorManager.removeBusyCursor();
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function updateFault(event:FaultEvent):void
		{
			facade.sendNotification(MintViewFacade.UPDATE_NOTICE_FAILED, this);
			CursorManager.removeBusyCursor();
		}
		
		public function fileInfoResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				//facade.sendNotification(MintViewFacade.FILEINFO_UPDATE_NOTICE_COMPLETE, this);
			}
			else {
				trace(event.result["RETCODE"]+" : "+event.result["RETTEXT"]);
				//facade.sendNotification(MintViewFacade.FILEINFO_UPDATE_NOTICE_FAILED, this);
			}
			CursorManager.removeBusyCursor();
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fileInfoFault(event:FaultEvent):void
		{
			//facade.sendNotification(MintViewFacade.FILEINFO_UPDATE_NOTICE_FAILED, this);
			CursorManager.removeBusyCursor();
		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////


		private function parseWriteList(srcArr:ArrayCollection):NoticeVO
		{
				
			var vo:NoticeVO = new NoticeVO();
			vo.AEDTM		= DateUtil.stringToDateTime(srcArr[0].AEDTM);
			vo.CDATE		= DateUtil.stringToDateTime(srcArr[0].CDATE);
			vo.CTIME		= DateUtil.stringToDateTime(srcArr[0].CTIME);
			vo.DATUM		= DateUtil.stringToDateTime(srcArr[0].DATUM);
			vo.ENAME 		= srcArr[0].ENAME;
			vo.ETIME		= DateUtil.stringToDateTime(srcArr[0].ETIME);
			vo.JINFO 		= srcArr[0].JINFO;
			vo.MANDT		= srcArr[0].MANDT;
			vo.MFLAG 		= srcArr[0].MFLAG;
			vo.ORGTX 		= srcArr[0].ORGTX;
			vo.PERNR 		= srcArr[0].PERNR;
			vo.SEQNR 		= srcArr[0].SEQNR;
			vo.SRTFD 		= srcArr[0].SRTFD;
			vo.JCTXT 		= srcArr[0].JCTXT;
			
			vo.STIME		= DateUtil.stringToDateTime(srcArr[0].STIME);
			vo.TITLE 		= srcArr[0].TITLE;
			vo.UZEIT		= DateUtil.stringToDateTime(srcArr[0].UZEIT);
			vo.VIEWR 		= srcArr[0].VIEWR;
			vo.VNAME		= srcArr[0].VNAME;
			vo.CONTENT		= srcArr[0].CONTENT;
				
				// 변경삭제 유무 
				if(vo.MFLAG == 'X' && vo.MFLAG == null&& vo.MFLAG == ""){
//					vo.MFLAG = true;
				} else {
//					vo.MFLAG = false;
				}	
				
			return vo;	
		}
		
		private function toJsonType(vo:mintListVO):Object {
			var paramObj:Object = new Object();

			paramObj.AEDTM		= DateUtil.toBasicString(vo.AEDTM);
			paramObj.CDATE		= DateUtil.toBasicString(vo.CDATE);
			paramObj.CTIME		= DateUtil.toBasicTimeString(vo.CTIME);
			paramObj.DATUM		= DateUtil.toBasicString(vo.DATUM);
			paramObj.STIME		= DateUtil.toBasicTimeString(vo.STIME);
			paramObj.ETIME		= DateUtil.toBasicTimeString(vo.ETIME);
			paramObj.ENAME		= vo.ENAME;		
			paramObj.JINFO		= vo.JINFO;		
			paramObj.MANDT		= vo.MANDT;		
			paramObj.MFLAG		= vo.MFLAG;	
			paramObj.ORGTX		= vo.ORGTX;	
			paramObj.PERNR		= vo.PERNR;
			paramObj.SEQNR		= vo.SEQNR;
			paramObj.SRTFD		= vo.SRTFD;	
			paramObj.TITLE		= vo.TITLE;
			paramObj.UZEIT		= DateUtil.toBasicTimeString(vo.UZEIT);
			paramObj.VIEWR		= vo.VIEWR;	
			paramObj.VNAME		= vo.VNAME;	
			return paramObj;
		}

		
	}
}